﻿$(function () {

    //固定南北 panel 分隔条
    $('#mainLayout').layout('panel', 'north').panel('panel').resizable('disable');
    //$('#mainLayout').layout('panel', 'south').panel('panel').resizable('disable');

    // LoadFirsUnReadMessage();

    LoadTopModules();
    //addTab('首页', '/PortalLTE/Main/Home', 'iconfont iconfont-shouye', false);
    //getEmailCount();
    //createRightBotDialog();
    //GetIntervalTime();

    
    // LoadFirsUnReadMessage();
});

function LoadTopModules() {
    commitAjax("/PortalLTE/Main/GetModules",
        {
            CallBack: function (data) {
                var dataObj = data;
                $.each(dataObj, function (i, e) {
                    var aHtml = "<div onclick='LoadAccordTree(\"" + e.id + "\",\"" + e.text + "\")' ";
                    aHtml += "class='easyui-menubutton' ";
                    aHtml += "data-options='iconCls:\"" + e.iconCls + "\",hasDownArrow:false'>" + e.text + "</a>";
                    $("#headerMenuLeft").append(aHtml);

                })
                $.parser.parse();

                if (dataObj.length > 0) {
                   LoadAccordTree(dataObj[0].id, dataObj[0].text);
                }
            }
        })
}

function LoadAccordTree(topId, topName) {
    //$('#mainHome').css('display', 'none');
    //$('#mainTab').css('visibility', 'visible');

    var $caption = $('<div class="theme-left-menu-caption"></div>')
                    .append('<span class="right iconfont theme-left-menu-switch">&#xf0025;</span>' + topName || '未命名');
    $('.theme-left-menu').html('');
    //$('.theme-left-menu').append($caption);
    commitAjax("/PortalLTE/Main/GetModuleMenus?id=" + topId,
         {
             CallBack: function (data) {
                 fixTreeData(data);
                 $('#LeftMenu').sidemenu({
                     data: data,
                     onSelect: onSideMenuSelect,
                     border: false,
                     animate: false
                 });
                 bindMenuEvent();
             }
         })
}

function fixTreeData(list) {
    $.each(list, function (index, item) {
        if (item.children.length > 0) {
            fixTreeData(item.children);
        }
        else {
            delete item.children;
        }
    })
}

function onTabSelect(title, index) {
    var tabs = $('#mainTab');
    var tab = tabs.tabs('getTab', index);
    var menus = $('#LeftMenu');
    if (menus.hasClass('sidemenu')) {
        var opts = menus.sidemenu("options");
        changeMenuSelect(menus, opts, tab[0].id);
    }
}

function onWestCollapse() {
    var opts = $('#LeftMenu').sidemenu('options');
    if (opts.collapsed != 'collapse') {
        $('#LeftMenu').sidemenu('collapse');
        $('#LeftMenu').sidemenu('resize', {
            width: 40
        });
    }
}

function onWestExpand() {
    var opts = $('#LeftMenu').sidemenu('options');
    if (opts.collapsed != 'expand') {
        $('#LeftMenu').sidemenu('expand');
        $('#LeftMenu').sidemenu('resize', {
            width: 200
        });
    }
}

function onSideMenuSelect(item) {
    if (!$('#mainTab').tabs('exists', item.text)) {
        $('#mainTab').tabs('add', {
            title: item.text,
            content: '<iframe scrolling="auto" frameborder="0"  src="' + item.url + '" style="width:100%;height:99%;"></iframe>',
            closable: true,
            //icon: item.iconCls,
            id: item.id
        });
    } else {
        $('#mainTab').tabs('select', item.text);
    }
    addTabMenu();
}

function addTabMenu() {
    /* 双击关闭TAB选项卡 */
    $(".tabs-inner").dblclick(function () {
        var subtitle = $(this).children(".tabs-closable").text();
        $('#mainTab').tabs('close', subtitle);
    });
    /* 为选项卡绑定右键 */
    $(".tabs-inner").bind('contextmenu', function (e) {
        $('#tab_menu').menu('show', {
            left: e.pageX,
            top: e.pageY
        });

        var subtitle = $(this).children(".tabs-closable").text();

        $('#tab_menu').data("currtab", subtitle);
        $('#mainTab').tabs('select', subtitle);
        return false;
    });
}

function changeMenuSelect(menus, opts, selectId) {
    var menutrees = menus.find(".sidemenu-tree");
    menutrees.each(function () {
        var menuItem = $(this);
        changeMenuStyle(menuItem, opts, selectId);
    });

    var tooltips = menus.find(".tooltip-f");
    tooltips.each(function () {
        var menuItem = $(this);
        var tip = menuItem.tooltip("tip");
        if (tip) {
            tip.find(".sidemenu-tree").each(function () {
                changeMenuStyle($(this), opts, selectId);
            });
            menuItem.tooltip("reposition");
        }
    });
}

function changeMenuStyle(menuItem, opts, selectId) {
    menuItem.find("div.tree-node-selected").removeClass("tree-node-selected");
    var node = menuItem.tree("find", selectId);
    if (node) {
        $(node.target).addClass("tree-node-selected");
        opts.selectedItemId = node.id;
        menuItem.trigger("mouseleave.sidemenu");
    }

    changeMenuSelect(menuItem);
}

function bindMenuEvent() {
    $('.theme-header-navigate-combobox').each(function () {
        $(this).combo('panel').panel({ cls: "theme-header-navigate-combobox-panel" });
    });

    var theme_left_layout = $(".theme-layout").layout("panel", 'west');
    var theme_left_menu_switch = true;
    $(".theme-left-menu-switch").on("click", function (event) {
        if (theme_left_menu_switch) {
            $(".theme-left-user-panel").hide(); /*隐藏左侧菜单用户面板*/
            $(".theme-left-menu dl").hide(); /*隐藏左侧子菜单*/

            theme_left_layout.panel('resize', { width: 36 });
            $(".theme-left-menu").css({ "width": "1px" });
            $(".theme-header-logo").hide();
            theme_left_menu_switch = false;
            $('#LeftMenu').sidemenu('collapse');
            $('#LeftMenu').sidemenu('resize', {
                width: 36
            });
        } else {
            $(".theme-left-user-panel").show(); /*显示左侧菜单用户面板*/
            $(".theme-left-menu dl").show(); /*显示左侧子菜单*/

            theme_left_layout.panel('resize', { width: 200 });
            $(".theme-left-menu").css({ "width": "180px" });
            $(".theme-header-logo").show();
            theme_left_menu_switch = true;
            $('#LeftMenu').sidemenu('expand');
            $('#LeftMenu').sidemenu('resize', {
                width: 200
            });
        }
        $(".theme-layout").layout('resize', { width: '100%' }); /*重置框架*/
    });

    var theme_left_menu_switch_hide = true;
    $(".theme-left-menu-switch-hide").on("click", function (event) {
        if (theme_left_menu_switch_hide) {
            //theme_left_layout.panel('resize',{width:1});
            $(".theme-layout").layout('remove', 'west');
            //$(".theme-left-layout").addClass('theme-left-layout-hide');
            theme_left_menu_switch_hide = false;
        } else {
            //theme_left_layout.panel('resize',{width:180});
            $(".theme-layout").layout('add', {
                region: 'west',
                bodyCls: 'theme-left-layout',
                href: 'menu_hide_left_content.html',
                border: false,
                width: 200
            });
            //$(".theme-left-layout").removeClass('theme-left-layout-hide');
            theme_left_menu_switch_hide = true;
        }
        $(".theme-layout").layout('resize', { width: '100%' }); /*重置框架*/
    });


    $(".theme-left-menu dl dt,.theme-inside-left-menu dl dt").on("click", function (event) {
        if (theme_left_menu_switch) {
            var node = $(this).next("dd");
            if (node.is(":hidden")) {
                node.show(); /*如果元素为隐藏,则将它显现*/
            } else {
                node.hide(); /*如果元素为显现,则将其隐藏*/
            }
        }
    });

    /*
	$(".theme-left-menu dl dt").on("mousemove",function(event) {
		if(!theme_left_menu_switch){
			var node=$(this).next("dd");
			node.addClass(".theme-left-menu-node-show");
			
		}
	});
	*/

    $(".theme-left-menu li").on("click", function (event) {
        $(".theme-left-menu li").removeClass("selected");
        $(this).addClass("selected");
    });
    $(".theme-inside-left-menu li").on("click", function (event) {
        $(".theme-inside-left-menu li").removeClass("selected");
        $(this).addClass("selected");
    });


    /*
	setInterval(function(){
		var nowDate  = new Date();
		var nowYear  = nowDate.getFullYear();
		var nowMonth = nowDate.getMonth().toString().length==1?"0"+nowDate.getMonth():nowDate.getMonth();
		var nowDays  = nowDate.getDate().toString().length==1?"0"+nowDate.getDate():nowDate.getDate();
		var nowHours = nowDate.getHours().toString().length==1?"0"+nowDate.getHours():nowDate.getHours();
		var nowMinute  = nowDate.getMinutes().toString().length==1?"0"+nowDate.getMinutes():nowDate.getMinutes();
		var nowSeconds  = nowDate.getSeconds().toString().length==1?"0"+nowDate.getSeconds():nowDate.getSeconds();
	    $("#theme-header-navigate-datetime").html(nowYear+"年"+nowMonth+"月"+nowDays+"日 "+nowHours+":"+nowMinute+":"+nowSeconds);
	},1000);
	*/


};

var taskInterval = null;
function beginTaskRotate(timeSpan)
{
    if (taskInterval)
        window.clearInterval(taskInterval);

    getToDoTaskCount();
    taskInterval = window.setInterval(function () { getToDoTaskCount() }, timeSpan || 5000);
}

function getToDoTaskCount() {   //获取未读消息条数，并显示在红圈中
    commitAjax("/PortalLTE/Main/GetMyToDoTaskCount",
      {
          CallBack: function (data) {
              if (data == 0) {
                  $('#taskCount').html('');
                  $('#taskCount').removeClass('haveCount');
              }
              else {
                  if (!$('#taskCount').hasClass('haveCount'))
                      $('#taskCount').addClass('haveCount');
                  $('#taskCount').html(data);
              }
          }
      })
}